package com.linktel.productionLine.controller;

import com.linktel.productionLine.common.BaseResponse;
import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.common.ResultUtils;
import com.linktel.productionLine.entity.dto.userProductionLine.RelationDTO;
import com.linktel.productionLine.entity.query.userProductionLine.AddRelationQuery;
import com.linktel.productionLine.entity.query.userProductionLine.DeleteRelationQuery;
import com.linktel.productionLine.exception.BusinessException;
import com.linktel.productionLine.service.CommitUserProductionLineService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 对 提交用户_产线 关联关系的有关请求
 *
 * @Author HuangJiaHui
 * @Date 2023/8/30 16:22
 */
@RestController
@RequestMapping("/user-production-line")
@AllArgsConstructor
public class UserProductionLineController {
    private CommitUserProductionLineService commitUserProductionLineService;

    /**
     * 查询所有 提交用户_产线 关联关系
     *
     * @return 提交用户_产线 关联关系列表
     */
    @GetMapping("/relation")
    public BaseResponse<List<RelationDTO>> listRelationByUserId(Long commitUserId) {
        List<RelationDTO> relationDTOList = commitUserProductionLineService.listRelationByUserId(commitUserId);
        return ResultUtils.success(relationDTOList);
    }

    /**
     * 新增 提交用户_产线 关联关系
     *
     * @param addRelationQuery  新增的提交用户_产线 关联关系的封装
     * @return 是否添加成功
     */
    @PostMapping("/relation")
    public BaseResponse<Boolean> addRelation(@RequestBody AddRelationQuery addRelationQuery) {
        Long commitUserId = addRelationQuery.getCommitUserId();
        String productionLineCode = addRelationQuery.getProductionLineCode();
        Long currentUserId = addRelationQuery.getCurrentUserId();
        if (commitUserId == null || commitUserId <= 0 || productionLineCode == null || productionLineCode.equals("") || currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "输入数据不完整");
        }
        Boolean result = commitUserProductionLineService.addRelation(commitUserId, productionLineCode, currentUserId);
        return ResultUtils.success(result);
    }

    /**
     * 删除 提交用户_产线 关联关系
     *
     * @param deleteRelationQuery  要删除的提交用户_产线 关联关系的封装
     * @return 是否删除成功
     */
    @DeleteMapping("/relation")
    public BaseResponse<Boolean> deleteRelation(@RequestBody DeleteRelationQuery deleteRelationQuery) {
        if (deleteRelationQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = deleteRelationQuery.getId();
        Long currentUserId = deleteRelationQuery.getCurrentUserId();

        if (id == null || id <= 0 || currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = commitUserProductionLineService.deleteRelation(id, currentUserId);
        return ResultUtils.success(result);
    }
}
